package com.artifact.mit.mypoint.async;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.artifact.mit.mypoint.http.URIResources;
import com.artifact.mit.mypoint.model.api.Promo;
import com.artifact.mit.mypoint.service.ServiceHandler;
import com.artifact.mit.mypoint.util.APICall.ImageLoader;
import com.artifact.mit.mypoint.util.SetImage;

public class PromoLoader extends AsyncTask<String, Void, Promo> {
		
	private TextView promoText1;
	private TextView promoText2;
	private TextView promoText3;
	private TextView promoText4;
	private ImageView promoImage1;
	private ImageView promoImage2;
	private ServiceHandler serviceHandler;
	
	public PromoLoader(Context context, TextView promoName1, TextView promoDesc1, ImageView promoImg1, TextView promoName2, TextView promoDesc2, ImageView promoImg2) {
		serviceHandler = new ServiceHandler(context);
		promoText1 = promoName1;
		promoText2 = promoDesc1;
		promoImage1 = promoImg1;
		promoText3 = promoName2;
		promoText4 = promoDesc2;
		promoImage2 = promoImg2;
	}
		
	@Override
	protected void onPostExecute(Promo result) {
		for (int i=0; i<result.getPromos().length; i++) {
			promoText1.setText(result.getPromos()[i].getName());
			promoText2.setText(result.getPromos()[i].getDesc());
			new ImageLoader().execute(new SetImage(promoImage1, appendImageUrl(result.getPromos()[i].getImage()), false));
			
			promoText3.setText(result.getPromos()[i+1].getName());
			promoText4.setText(result.getPromos()[i+1].getDesc());
			new ImageLoader().execute(new SetImage(promoImage2, appendImageUrl(result.getPromos()[i+1].getImage()), false));
			
			break;
		}
	}
	
	private String appendImageUrl(String url) {		
		return URIResources.MAIN_URL.concat(url.replace("~", ""));
	}
	
	@Override
	protected Promo doInBackground(String... params) {
		try {
			return serviceHandler.promoService();
		} catch (Exception e) {
			Log.e(this.getClass().getName(), "Error Promo Service", e);
		}
		return null;
	}
}
